package com.modelscope.ai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.modelscope.ai.domain.dto.DikeQueryCriteria;
import com.modelscope.ai.domain.entity.Dike;
import com.modelscope.ai.domain.entity.User;
import com.modelscope.ai.service.DikeService;
import com.modelscope.ai.mapper.DikeMapper;
import com.modelscope.ai.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 20346
 * @description 针对表【dike(大坝)】的数据库操作Service实现
 * @createDate 2024-02-21 20:40:29
 */
@Service
public class DikeServiceImpl extends ServiceImpl<DikeMapper, Dike> implements DikeService {

    @Override
    public Page<Dike> queryAll(DikeQueryCriteria dikeQueryCriteria) {
        // 用来返回所有用户信息，便于测试
        return page(
                new Page<>(dikeQueryCriteria.getPageNum(), dikeQueryCriteria.getPageSize()),
                new LambdaQueryWrapper<Dike>().like(StringUtils.isNotBlank(dikeQueryCriteria.getName()),Dike::getName, dikeQueryCriteria.getName())
        );
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(Dike dike) {
        AssertUtil.isEmpty(dike.getId(), "Id应该为空");
        save(dike);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(Dike dike) {
        AssertUtil.isNotEmpty(dike.getId(), "Id为空");
        Dike one = lambdaQuery().eq(Dike::getId, dike.getId()).one();
        AssertUtil.isNotEmpty(one, "对不起id不存在");
        saveOrUpdate(dike);

    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void delete(Long ids) {
        Dike one = lambdaQuery().eq(Dike::getId, ids).one();
        AssertUtil.isNotEmpty(one, "对不起id不存在");
        removeById(ids);
    }
}




